package com.felix.netty.c1.byteBuf;

import io.netty.buffer.*;

import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty.util.internal.StringUtil.NEWLINE;

/** 很多地方体现零拷贝，例如 slice、duplicate、CompositeByteBuf、 Unpooled**/
public class example2 {
    private static void log(ByteBuf buffer) {
        int length = buffer.readableBytes();
        int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
        StringBuilder buf = new StringBuilder(rows * 80 * 2)
                .append("read index:").append(buffer.readerIndex())
                .append(" write index:").append(buffer.writerIndex())
                .append(" capacity:").append(buffer.capacity())
                .append(NEWLINE);
        appendPrettyHexDump(buf, buffer);
        System.out.println(buf.toString());
    }

    public static void main(String[] args) {
        //        System.out.println("****slice*****");
//        ByteBuf origin = ByteBufAllocator.DEFAULT.buffer(10);
//        origin.writeBytes(new byte[]{1,2,3,4});
//        origin.readByte();
//        System.out.println(ByteBufUtil.prettyHexDump(origin));
//

//        ByteBuf slice = origin.slice();
//        System.out.println(ByteBufUtil.prettyHexDump(slice));

//        origin.readByte();
//        System.out.println(ByteBufUtil.prettyHexDump(origin));
//        log(slice);

//        slice.setByte(2, 5);
//        System.out.println(ByteBufUtil.prettyHexDump(origin));

//        System.out.println("****duplicate*****");
//        ByteBuf buf1 = ByteBufAllocator.DEFAULT.buffer(5);
//        buf1.writeBytes(new byte[]{1, 2, 3, 4, 5});
//        ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer(5);
//        buf2.writeBytes(new byte[]{6, 7, 8, 9, 10});
//        System.out.println(ByteBufUtil.prettyHexDump(buf1));
//        System.out.println(ByteBufUtil.prettyHexDump(buf2));

        /** 复制内存方式 **/
//        ByteBuf buf3 = ByteBufAllocator.DEFAULT.buffer(buf1.readableBytes() + buf2.readableBytes());
//        buf3.writeBytes(buf1);
//        buf3.writeBytes(buf2);
//        System.out.print("buf1:");
//        log(buf1);
//        System.out.print("buf2:");
//        log(buf2);
//        System.out.println(ByteBufUtil.prettyHexDump(buf3));

//        CompositeByteBuf buf4 = ByteBufAllocator.DEFAULT.compositeBuffer();
//        buf4.addComponents(true,buf1,buf2);
//        System.out.print("buf1:");
//        log(buf1);
//        System.out.print("buf2:");
//        log(buf2);
//        System.out.println("buf4:"+ByteBufUtil.prettyHexDump(buf4));

        System.out.println("****Unpooled*****");
        ByteBuf buf1 = ByteBufAllocator.DEFAULT.buffer(5);
        buf1.writeBytes(new byte[]{1, 2, 3, 4, 5});
        ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer(5);
        buf2.writeBytes(new byte[]{6, 7, 8, 9, 10});

        // 当包装 ByteBuf 个数超过一个时, 底层使用了 CompositeByteBuf
        ByteBuf buf3 = Unpooled.wrappedBuffer(buf1, buf2);
        System.out.println(ByteBufUtil.prettyHexDump(buf3));

        ByteBuf buf4 = Unpooled.wrappedBuffer(new byte[]{1, 2, 3}, new byte[]{4, 5, 6});
        System.out.println(buf4.getClass());
        System.out.println(ByteBufUtil.prettyHexDump(buf4));
    }
}
